package com.fzy.hot.ListNode;

/**
 * 反转范围内的链表
 */
public class Test02 {
    /**
     *
     * @param head ListNode类
     * @param m int整型
     * @param n int整型
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = head;
        ListNode pre = dummy;

        for(int i = 0;i<m;i++){
            pre = cur;
            cur = cur.next;
        }


        for (int i = m;i<=n;i++){
            ListNode next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return dummy.next;
    }
}
